﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;
public partial class admin_OrderDetail : SitePage
{
    busStoreOrder _busOrder = new busStoreOrder();
    busCustomer _busCus = new busCustomer();
    busLocation _busLocation = new busLocation();
    busStoreOrderDetail _busOrderDetail = new busStoreOrderDetail();
    #region Fields



    public int OrderId
    {

        get
        {
            object o = ViewState["OrderId"];
            if (o != null) return (int)o;
            return int.MinValue;
        }

        set { ViewState["OrderId"] = value; }

    }
    #endregion

    #region Init,Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.OrderId = clsQueryString.GetInt("OrderId");
            LoadDataOrder();

        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);

    }
    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);

    }
    #endregion

    #region Methods
    private string DrawProcess(Location objLocation,StoreOrderDetail objOrderDetail)
    {
        int maxValue = 100;        
        clsSQL sql = new clsSQL();
        int countFinish = (int)sql.ExecuteScalar("Select COALESCE(sum(Quantity),0) from Entry where OrderDetailId=" + objOrderDetail.OrderDetailId.ToString() + " and LocationId=" + objLocation.LocationId.ToString());
        string itemFinish = countFinish.ToString("###,###,###");
        if (string.IsNullOrEmpty(itemFinish))
            itemFinish="0";
        double pComplete = (countFinish * 1.0 / objOrderDetail.Quantity * 1.0);
        int valueComplete = (int)(pComplete * maxValue);
        if (valueComplete < 0)
            valueComplete = 0;
        if (valueComplete > maxValue)
            valueComplete = maxValue;
        string imgAdd = "<img src='/App_Themes/Admin/images/add.png' />";
        if (countFinish > 0 && objLocation.LocationId == WebUtil.Common.LocationThietKe)
            imgAdd = string.Empty;
        else if (valueComplete == maxValue)
            imgAdd = string.Empty;
        return @"<td class='progress'>
                        <span class='loc'>" + objLocation.Name + @"</span>
                        <progress value='" + valueComplete + "' max='" + maxValue + @"'></progress>
                        <div class='pa'>
                            <a href='EntryAdd.aspx?backURL=OrderDetail.aspx?OrderId=" + this.OrderId + "&OrderDetailId=" + objOrderDetail.OrderDetailId + "&LocationId=" + objLocation.LocationId + @"'>" + imgAdd + @"</a>
                            <span class='qty'> <a href='EntryList.aspx?OrderDetailId=" + objOrderDetail.OrderDetailId + "&LocationId=" + objLocation.LocationId + @"'>" + itemFinish + @"</a></span>
                        </div>
                    </td>";
    }
    private void LoadListOrderDetail()
    {
        StoreOrderDetailCollection lstDetail = _busOrderDetail.ListByOrder(this.OrderId);
        if (lstDetail != null)
        {
            string paraURL = GetPageParams(FilterFieldType.All);
            if (lstDetail.Count > 0)
            {
                LocationCollection lstLocation = _busLocation.GetAll();
                string processHTML = string.Empty;
                string tableCSS = string.Empty;
                for (int i = 0; i < lstDetail.Count; i++)
                {
                    StoreOrderDetail objDetail = lstDetail[i];
                    if (i % 2 == 0)
                        tableCSS = "tabpro";
                    else
                        tableCSS = "tabpro2";
                    processHTML = @" <table border='0' cellpadding='0' cellspacing='0' class='" + tableCSS + @"'>
                                        <tr>
                                            <td valign='top' style='width:200px' class='designcode'>
                                                " + objDetail.DesignCode + @" <a href='OrderDetailAdd.aspx?OrderDetailId=" + objDetail.OrderDetailId + "&OrderId=" + objDetail.OrderId + @"'><img src='/App_Themes/Admin/images/edit.png' /></a>
                                                <br />
                                                <div class='qty'>" +  objDetail.Quantity.ToString("###,###,###") + @" cái</div>
                                            </td>";
                    if (lstLocation != null)
                    {
                        if (lstLocation.Count > 0)
                        {
                            foreach (Location objLocation in lstLocation)
                            {
                                processHTML += DrawProcess(objLocation,objDetail);
                            }
                        }
                    }
                    processHTML +="</tr></table>";
                    ltrProcess.Text+= processHTML;
                }               
            }
        }
    }
    private void LoadDataOrder()
    {
        StoreOrder objOrder = _busOrder.GetByID(this.OrderId);
        if (objOrder != null)
        {
            litPurchaseOrder.Text = objOrder.PurchaseOrder;
            litTrackingOrder.Text = objOrder.TrackingOrder;
            if (objOrder.FinishDate != DateTime.MinValue)
                litFinishDate.Text = clsConvert.String(objOrder.FinishDate);
            else litFinishDate.Text = "";
            litCustomer.Text = string.Empty;
            if (objOrder.CustomerId > 0)
            {
                Customer objCus = _busCus.GetById(objOrder.CustomerId);
                if (objCus != null)
                {
                    litCustomer.Text = objCus.Name.Trim();
                }
            }
            litCreatedDate.Text = string.Empty;
            if (objOrder.CreatedDate != DateTime.MinValue)
            {
                litCreatedDate.Text = objOrder.CreatedDate.ToString("yyyy/MM/dd h:mm:ss tt");
            }
            litCreatedBy.Text = objOrder.CreatedBy;
            litModifiedDate.Text = string.Empty;
            if (objOrder.ModifiedDate != DateTime.MinValue)
            {
                litModifiedDate.Text = objOrder.ModifiedDate.ToString("yyyy/MM/dd h:mm:ss tt");
            }
            litModifiedBy.Text = objOrder.CreatedBy;
            LoadListOrderDetail();
        }
    }
    #endregion

    #region Events

    protected void btnAddOrderDetail_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrderDetailAdd.aspx?" + this.GetPageQuery);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("OrderList.aspx?" + this.GetPageQuery);
    }

    #endregion

}